import { renderIcon } from '@/utils/index';

export function generateMenuOptions(routes: any[], basePath = ''): any[] {
  return routes
    .filter((r) => !r.hidden)
    .map((r) => {
      // 拼接完整路径
      const { meta } = r;
      const fullPath = meta?.link || basePath + '/' + r.path.replace(/^\//, '');

      const item: any = {
        label: meta?.title,
        key: meta?.activeMenu || fullPath, // 也可以直接用 key
        routePath: fullPath, // 保存完整路由路径
        icon: meta?.icon ? renderIcon(meta?.icon) : null,
        isLink: !!meta?.link, // 是否是外链
      };

      if (r.children && r.children.length > 0) {
        item.children = generateMenuOptions(r.children, fullPath);
      }

      return item;
    });
}
